package com.tj.collection.controller.admin;

import com.fqgj.common.api.annotations.ParamsValidate;
import com.tj.collection.bean.ApiResponse;
import com.tj.collection.controller.admin.request.AddAdminRoleVo;
import com.tj.collection.controller.admin.request.AddOperationVo;
import com.tj.collection.controller.admin.request.AddRoleMenuVo;
import com.tj.collection.controller.admin.request.AddRoleOperationVo;
import com.tj.collection.controller.admin.response.RoleOperationIdsResponse;
import com.tj.collection.db.service.AdminMenuService;
import com.tj.collection.db.service.AdminPermissionService;
import com.tj.collection.db.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2016/11/18
 * Time: 下午5:44
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private AdminMenuService adminMenuService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private AdminPermissionService adminPermissionService;


    /**
     * 关联角色和菜单
     *
     * @param addRoleMenuVo
     * @return
     */
    @RequestMapping("/role/menu")
    @ParamsValidate
    public ApiResponse roleMenu(@RequestBody AddRoleMenuVo addRoleMenuVo) {
        adminMenuService.addMenuIdToRole(addRoleMenuVo.getRoleId(), addRoleMenuVo.getMenuIds());
        return new ApiResponse<>();
    }

    /**
     * 关联管理员和角色
     *
     * @param addAdminRoleVo
     * @return
     */
    @RequestMapping("/admin/role")
    @ParamsValidate
    public ApiResponse adminRole(@RequestBody AddAdminRoleVo addAdminRoleVo) {
        adminRoleService.addRoleToAdmin(addAdminRoleVo.getAdminId(), addAdminRoleVo.getRoleIds());
        return new ApiResponse<>();
    }


    @RequestMapping("/operation/add")
    @ParamsValidate
    public ApiResponse addOperation(@RequestBody AddOperationVo addOperationVo) {
        adminPermissionService.addPermission(addOperationVo.trans2Permission());
        return new ApiResponse<>();
    }

    @RequestMapping("/role/operation")
    @ParamsValidate
    public ApiResponse roleOperation(@RequestBody AddRoleOperationVo addOperationVo) {
        List<Long> ids = new ArrayList<>();
        addOperationVo.validate();
        for (String operationId : addOperationVo.getOperationIds()) {
            ids.add(Long.parseLong(operationId));
        }

        adminPermissionService.addPermissionToRole(addOperationVo.getRoleId(), ids);
        return new ApiResponse<>();
    }

    @RequestMapping("/role/{roleId}/operation/ids")
    public ApiResponse roleOperationList(@PathVariable Long roleId) {
        List<Long> permissionIds = adminPermissionService.getPermissionIdsByRoleId(roleId);

        return new ApiResponse<>(new RoleOperationIdsResponse(permissionIds));
    }
}
